﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using UnityEngine.UI;

public class UIPrepare : MonoBehaviour
{
    public GameObject camera, uiFly, gameStart, right;

    public GameObject objRight, objGold, btnUp, btnMid, btnBottom;
    public Text textUp, textMid, textB, textGold;
    //textUp, textZTQ, textZR, textMid, textHJ, textCK, textTY, textB, textWY, textSD,
    public Slider sliderUp, sliderMid, sliderB;
    public GameObject rocket, objStep, objPeople, objJiazi;
    public GameObject particleUp, particleDown, coverUp, coverDown;
    private GameObject rocketDoor;

    // Start is called before the first frame update
    void Start()
    {
        rocketDoor = rocket.transform.GetChild(0).GetChild(0).GetChild(0).gameObject;
        textUp.text = "Lv" + DataController.Instance.up.ToString();
        sliderUp.value = DataController.Instance.up % 5 / 5.0f;
        textMid.text = "Lv" + DataController.Instance.mid.ToString();
        sliderMid.value = DataController.Instance.mid % 5 / 5.0f;
        textB.text = "Lv" + DataController.Instance.bottom.ToString();
        sliderB.value = DataController.Instance.bottom % 5 / 5.0f;
        textGold.text = DataController.Instance.gold.ToString();
        UGUIEventListener.Get(btnUp).onClick = (obj) =>
        {
            if (DataController.Instance.gold >= 50)
            {
                DataController.Instance.gold -= 50;
                DataController.Instance.up += 1;
                textUp.text = "Lv" + DataController.Instance.up.ToString();
                sliderUp.value = DataController.Instance.up % 5 / 5.0f;
                textGold.text = DataController.Instance.gold.ToString();
                if (DataController.Instance.up == 5 || DataController.Instance.up == 10 || DataController.Instance.up == 15)
                {
                    HideUI();
                    HuoJianPeople();
                }
                else
                {
                    particleUp.GetComponent<ParticleSystem>().Play();
                    coverUp.GetComponent<Renderer>().materials[0].SetFloat("_animation", 0f);
                    coverUp.GetComponent<Renderer>().materials[0].DOFloat(1, "_animation", 1f);
                }
            }
        };
        UGUIEventListener.Get(btnMid).onClick = (obj) =>
        {
            if (DataController.Instance.gold >= 50)
            {
                DataController.Instance.gold -= 50;
                DataController.Instance.mid += 1;
                textMid.text = "Lv" + DataController.Instance.mid.ToString();
                sliderMid.value = DataController.Instance.mid % 5 / 5.0f;
                textGold.text = DataController.Instance.gold.ToString();
                if (DataController.Instance.mid == 5 || DataController.Instance.mid == 10)
                {
                    HideUI();
                    JiaShangMian(DataController.Instance.mid / 5 + 1);
                }
                else
                {
                    particleUp.GetComponent<ParticleSystem>().Play();
                    coverUp.GetComponent<Renderer>().materials[0].SetFloat("_animation", 0f);
                    coverUp.GetComponent<Renderer>().materials[0].DOFloat(1, "_animation", 1f);
                }
            }
        };
        UGUIEventListener.Get(btnBottom).onClick = (obj) =>
        {
            if (DataController.Instance.gold >= 50)
            {
                DataController.Instance.gold -= 50;
                DataController.Instance.bottom += 1;
                textB.text = "Lv" + DataController.Instance.bottom.ToString();
                sliderB.value = DataController.Instance.bottom % 5 / 5.0f;
                textGold.text = DataController.Instance.gold.ToString();
                if (DataController.Instance.bottom == 5 || DataController.Instance.bottom == 10)
                {
                    HideUI();
                    JiaXiamian(DataController.Instance.bottom / 5 + 1);
                }
                else
                {
                    particleDown.GetComponent<ParticleSystem>().Play();
                    coverDown.GetComponent<Renderer>().materials[0].SetFloat("_animation", 0f);
                    coverDown.GetComponent<Renderer>().materials[0].DOFloat(1, "_animation", 1f);
                }
            }
        };
        UGUIEventListener.Get(gameStart).onClick = (obj) =>
        {
            gameStart.SetActive(false);
            Sequence s = DOTween.Sequence();
            s.Append(camera.transform.DOMove(GameController.instance.positionFly.position, 1))
            .Join(right.GetComponent<RectTransform>().DOAnchorPosX(1650, 1))
            .OnComplete(() =>
            {
                camera.GetComponent<follow>().distance = camera.transform.position - GameController.instance.rocket.transform.position;
                camera.GetComponent<follow>().canFollow = true;
                GameController.instance.rocket.GetComponent<rocket>().StartGame();
                uiFly.SetActive(true);
                gameObject.SetActive(false);
            });
        };
    }

    public void Reset()
    {
        gameStart.SetActive(true);
        camera.transform.position = GameController.instance.positionSit.position;
        right.GetComponent<RectTransform>().DOAnchorPosX(-475, 0.8f);
    }

    public void HideUI()
    {
        objRight.GetComponent<RectTransform>().DOAnchorPosX(600, 0.5f);
        gameStart.GetComponent<RectTransform>().DOAnchorPosY(-250, 0.5f);
        objGold.GetComponent<RectTransform>().DOAnchorPosX(-360, 0.5f);
    }
    public void BackUI()
    {
        camera.transform.DOMoveZ(12.5f, 0.5f);
        objRight.GetComponent<RectTransform>().DOAnchorPosX(-475, 0.5f);
        gameStart.GetComponent<RectTransform>().DOAnchorPosY(465, 0.5f);
        objGold.GetComponent<RectTransform>().DOAnchorPosX(250, 0.5f);
    }

    public void HuoJianPeople()
    {
        rocketDoor.GetComponent<Animation>().Play("open");
        GameObject step = Instantiate(objStep, new Vector3(0, 2.7f, 0), Quaternion.identity);
        step.GetComponent<Animation>().Play("step_open");
        GameObject people = Instantiate(objPeople, new Vector3(-4.8f, 0, 2.3f), Quaternion.Euler(new Vector3(0, 90, 0)));
        Sequence sequence = DOTween.Sequence();
        sequence.Append(people.transform.DOMove(new Vector3(0, 0, 2.3f), 3).SetEase(Ease.Linear));
        sequence.AppendCallback(() => { people.GetComponent<Animation>().CrossFade("standby"); });
        sequence.Append(people.transform.DOLocalRotate(new Vector3(0, 180, 0), 0.2f));
        sequence.Append(people.transform.DOMove(new Vector3(0, 2.8f, 2.3f), 2).SetEase(Ease.Linear));
        sequence.Append(people.transform.DOMove(new Vector3(0, 2.8f, 0f), 0.5f).SetEase(Ease.Linear));
        sequence.Append(people.transform.DOLocalRotate(new Vector3(0, 0, 0), 0.2f));
        sequence.AppendCallback(() => { step.GetComponent<Animation>().Play("step_close"); });
        sequence.AppendInterval(1.3f);
        sequence.AppendCallback(() => { rocketDoor.GetComponent<Animation>().Play("close"); });
        sequence.AppendInterval(0.5f);
        sequence.OnComplete(() => { Destroy(people); Destroy(step); BackUI(); });
    }

    public void JiaShangMian(int lv)
    {
        camera.transform.DOMoveZ(25, 1f);
        GameObject jiazi = Instantiate(objJiazi, new Vector3(8, 3.5f, 0), Quaternion.Euler(new Vector3(90, 0, 0)));
        GameObject huojianTou1 = null;
        GameObject huojianTou2 = null;
        Sequence sequence = DOTween.Sequence();
        sequence.Append(jiazi.transform.DOMove(new Vector3(3f, 3.5f, 0), 1f).SetEase(Ease.Linear));
        sequence.Join(jiazi.transform.DOLocalRotate(new Vector3(360, 0, 0), 1f, RotateMode.FastBeyond360));
        sequence.AppendInterval(0.35f);
        sequence.Append(jiazi.transform.DOMove(new Vector3(1.5f, 3.5f, 0), 1).SetEase(Ease.Linear));
        sequence.AppendCallback(() =>
        {
            huojianTou1 = rocket.transform.GetChild(0).gameObject;
            huojianTou1.transform.SetParent(jiazi.transform);
        });
        sequence.AppendInterval(0.8f);
        sequence.AppendCallback(() =>
        {
            Camera.main.DOShakePosition(0.3f, Vector3.one * 0.2f, vibrato: 100);
        });
        sequence.Join(jiazi.transform.DOMove(new Vector3(1.5f, 5f, 0), 0.5f).SetEase(Ease.OutBack));
        sequence.AppendInterval(0.5f);
        sequence.Append(jiazi.transform.DOMove(new Vector3(-1.5f, 5f, 0), 0.5f).SetEase(Ease.InOutQuad));
        sequence.Join(jiazi.transform.DOLocalRotate(new Vector3(-90, 180, 0), 0.5f).SetEase(Ease.Linear));
        sequence.AppendInterval(0.5f);
        sequence.Append(jiazi.transform.DOMove(new Vector3(8, 5, 0), 1).SetEase(Ease.Linear));
        sequence.AppendCallback(() =>
        {
            jiazi.transform.position = new Vector3(8, 5, 0);
            jiazi.transform.rotation = Quaternion.Euler(new Vector3(90, 0, 0));
            Destroy(huojianTou1);
            huojianTou2 = Instantiate(Resources.Load("R_LV0" + lv.ToString() + "_top") as GameObject);
            huojianTou2.transform.SetParent(jiazi.transform);
            huojianTou2.transform.localPosition = new Vector3(-1.5f, -0.8f, 0);
            huojianTou2.transform.localRotation = Quaternion.Euler(Vector3.zero);
        });
        sequence.AppendInterval(0.8f);
        sequence.Append(jiazi.transform.DOMove(new Vector3(1.5f, 5f, 0), 2));
        sequence.Append(jiazi.transform.DOLocalRotate(Vector3.zero, 0.5f));
        sequence.Append(jiazi.transform.DOLocalMoveY(3.5f, 0.5f).SetEase(Ease.InQuart));
        sequence.AppendCallback(() =>
        {
            Camera.main.DOShakePosition(0.3f, Vector3.one * 0.2f, vibrato: 100);
        });
        sequence.AppendInterval(0.5f);
        sequence.AppendCallback(() =>
        {
            huojianTou2.transform.SetParent(rocket.transform);
            huojianTou2.transform.SetSiblingIndex(0);
        });
        sequence.Append(jiazi.transform.DOMove(new Vector3(8, 3.5f, 0), 2));
        sequence.OnComplete(() => {
            Destroy(jiazi);
            BackUI();
            GameController.instance.rocket.GetComponent<rocket>().ResetRocket1and2();
            rocketDoor = huojianTou2.transform.GetChild(0).GetChild(0).gameObject;
        });
    }

    public void JiaXiamian(int lv)
    {
        camera.transform.DOMoveZ(25, 1f);
        GameObject jiazi = Instantiate(objJiazi, new Vector3(8, 2.2f, 0), Quaternion.Euler(new Vector3(90, 0, 0)));  //1.8
        GameObject huojianWei1 = null;
        GameObject huojianWei2 = null;
        Sequence sequence = DOTween.Sequence();
        sequence.Append(jiazi.transform.DOMove(new Vector3(3f, 2.2f, 0), 1));
        sequence.Join(jiazi.transform.DOLocalRotate(new Vector3(0, 0, 0), 1f));
        sequence.AppendInterval(0.35f);
        sequence.Append(jiazi.transform.DOMove(new Vector3(1.5f, 2.2f, 0), 1).SetEase(Ease.Linear));
        sequence.AppendCallback(() =>
        {
            huojianWei1 = rocket.transform.GetChild(1).gameObject;
            huojianWei1.transform.SetParent(jiazi.transform);
        });
        sequence.AppendInterval(0.8f);
        sequence.AppendCallback(() =>
        {
            Camera.main.DOShakePosition(0.3f, Vector3.one * 0.2f, vibrato: 100);
        });
        sequence.Join(jiazi.transform.DOMove(new Vector3(1.5f, 1.75f, 0), 0.5f).SetEase(Ease.OutBack));
        sequence.AppendInterval(0.5f);
        sequence.Append(jiazi.transform.DOMove(new Vector3(8, 1.75f, 0), 1).SetEase(Ease.Linear));
        sequence.AppendCallback(() =>
        {
            Destroy(huojianWei1);
            huojianWei2 = Instantiate(Resources.Load("R_LV0" + lv.ToString() + "_bottom") as GameObject);
            huojianWei2.transform.SetParent(jiazi.transform);
            huojianWei2.transform.localPosition = new Vector3(-1.5f, 0.5f, 0);
            huojianWei2.transform.localRotation = Quaternion.Euler(Vector3.zero);
        });
        sequence.AppendInterval(0.8f);
        sequence.Append(jiazi.transform.DOMove(new Vector3(1.5f, 1.75f, 0), 2));
        sequence.Append(jiazi.transform.DOLocalMoveY(2.2f, 0.5f).SetEase(Ease.InQuart));
        sequence.AppendCallback(() =>
        {
            Camera.main.DOShakePosition(0.3f, Vector3.one * 0.2f, vibrato: 100);
        });
        sequence.AppendInterval(0.5f);
        sequence.AppendCallback(() =>
        {
            huojianWei2.transform.SetParent(rocket.transform);
            huojianWei2.transform.SetSiblingIndex(1);
        });
        sequence.Append(jiazi.transform.DOMove(new Vector3(8, 2.2f, 0), 2));
        sequence.OnComplete(() => { Destroy(jiazi); BackUI(); GameController.instance.rocket.GetComponent<rocket>().ResetRocket1and2(); });
    }

    //void InitUPText()
    //{
    //    textUp.text = "头部等级：" + DataController.Instance.up.ToString();
    //    textZTQ.text = "助推器：" + DataController.Instance.maxPower3.ToString();
    //    textZR.text = "最大载人：" + DataController.Instance.people.ToString();
    //    textGold.text = "金币：" + DataController.Instance.gold.ToString();
    //}

    //void InitMIDText()
    //{
    //    textMid.text = "主体等级：" + DataController.Instance.mid.ToString();
    //    textHJ.text = "护甲：" + DataController.Instance.maxHP.ToString();
    //    textTY.text = "备用燃油：" + DataController.Instance.maxPower2.ToString();
    //    textCK.text = "灵敏度：" + DataController.Instance.sensitive.ToString();
    //    textGold.text = "金币：" + DataController.Instance.gold.ToString();
    //}

    //void InitBText()
    //{
    //    textB.text = "尾部等级：" + DataController.Instance.bottom.ToString();
    //    textWY.text = "主油箱：" + DataController.Instance.maxPower1.ToString();
    //    textSD.text = "最大速度：" + DataController.Instance.maxSpeed.ToString();
    //    textGold.text = "金币：" + DataController.Instance.gold.ToString();
    //}
}